<?php
/**
 * 视图类
 */
class View {
    private $request   = NULL;
    private $data      = array ();

	public $baseUrl;
	public $fullUrl;
	public $controllerName;
    /**
     * 初始化
     */
    public function __construct ($request) {
        $this->request = $request;
    }


    /**
     * 渲染视图
     *
     * @param $scriptPath 脚步路径
     */
    public function render ($scriptPath) {
        if (!file_exists ($scriptPath))
        $this->run ($this->request->getViewPath () . '/' . $scriptPath);
        else
        $this->run ($scriptPath);
    }


    /**
     * 渲染视图
     *
     * @param $scriptPath 视图路径
     */
    public function run ($scriptPath) {
    	$data = $this->data;
    	
        include $scriptPath;
    }


    public function __set ($key, $v) {
        $this->data [$key] = $v;
    }


    public function __get ($key) {
        $r = isset ($this->data [$key]) ? $this->data [$key] : '';
        return $r;
    }


    /**
     * empty 与isset正常工作
     */
    public function __isset ($key) {
        return isset ($this->data [$key]);
    }


  /**
     * 输出url地址，所有本程序的地址都有此方法输出
     * @param <String> $baseUrl 相对地址
     * @param <String> $controller 控制器名称
     * @param <String> $action 方法名称
     * @param <String> $param 参数
     * @param <String> $module 模块名称
     * @return <String> 地址
     */
    public function url ($baseUrl, $controller='index', $action='index', $param='', $module='') {
        return $this->request->url ($baseUrl, $controller, $action, $param, $module);
    }
}